/**
 * 
 */
package de.midnightdance.admin.clan;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import de.cbf.util.ui.editentities.controller.ControllerEditEntity;
import de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity;
import de.cbf.util.ui.editentities.interfaces.IEditableEntity;
import de.midnightdance.admin.ws.entities.Clan;

/**
 * @author Stefan Alter
 *
 */
public class PresenterEditClan extends ControllerEditEntity {

	private PCLClan pcl;	
	
	private ListenerModifiedName		listenerModifiedName		;
	private ListenerModifiedNotizen		listenerModifiedNotizen		;
	/**
	 * 
	 */
	public PresenterEditClan() {
		super();
		pcl	= new PCLClan();
		//
		listenerModifiedName		= new ListenerModifiedName();
		listenerModifiedNotizen		= new ListenerModifiedNotizen();	
	}
	
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#checkEntity(de.cbf.util.ui.editentities.interfaces.IEditableEntity)
	 */
	@Override
	protected boolean checkEntity(IEditableEntity value)
			throws IllegalArgumentException {
		return (value instanceof Clan);
	}
	
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#checkView(de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity)
	 */
	@Override
	protected boolean checkView(ICompositeEditEntity value)
			throws IllegalArgumentException {
		return (value instanceof CompositeEditClan);		
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#registerViewListenersAndInit(de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity)
	 */
	@Override
	protected boolean registerViewListenersAndInit(ICompositeEditEntity value) {
		if (value instanceof CompositeEditClan) {
			CompositeEditClan composite = (CompositeEditClan) value;
			composite.registerModifiedNameListener	(listenerModifiedName);
			composite.registerModfiedNotizenListener(listenerModifiedNotizen);					
			return true;
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#deRegisterViewListeners(de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity)
	 */
	@Override
	protected boolean deRegisterViewListeners(ICompositeEditEntity value) {
		if (value instanceof CompositeEditClan) {
			CompositeEditClan composite = (CompositeEditClan) value;
			composite.deRegisterModifiedNameListener	(listenerModifiedName);
			composite.deRegisterModfiedNotizenListener	(listenerModifiedNotizen);					
			return true;
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#getPCLforEntity()
	 */
	@Override
	protected PropertyChangeListener getPCLforEntity() {
		return pcl;
	}
	
	public void modifiedName() {
		if ((entity instanceof Clan)&& (view instanceof CompositeEditClan)) {
			Clan typedEntity = (Clan) entity;
			CompositeEditClan composite = (CompositeEditClan)view;
			//
			typedEntity.setName(composite.getClanName());
			//
			testUnsavedChanges();
		}	
	}
	public void modifiedNotizen() {
		if ((entity instanceof Clan)&& (view instanceof CompositeEditClan)) {
			Clan typedEntity = (Clan) entity;
			CompositeEditClan composite = (CompositeEditClan)view;
			//
			typedEntity.setNotizen(composite.getNotizen());
			//
			testUnsavedChanges();
		}		
	}	
	
	public void updateName() {
		if ((entity instanceof Clan)&& (view instanceof CompositeEditClan)) {
			Clan typedEntity = (Clan) entity;
			CompositeEditClan composite = (CompositeEditClan)view;
			//
			composite.setClanName(typedEntity.getName());			
		}	
	}
	public void updateNotizen() {
		if ((entity instanceof Clan)&& (view instanceof CompositeEditClan)) {
			Clan typedEntity = (Clan) entity;
			CompositeEditClan composite = (CompositeEditClan)view;
			//
			composite.setNotizen(typedEntity.getNotizen());
		}	
	}
	
	private class PCLClan implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {			
			if (evt instanceof IndexedPropertyChangeEvent) {
				IndexedPropertyChangeEvent idxEvent = (IndexedPropertyChangeEvent) evt;
				switch (idxEvent.getIndex()) {					
					case Clan.PCE_INDEX_Name 	:{
						updateName();
						break;
					}
					case Clan.PCE_INDEX_Notizen 	:{
						updateNotizen();
						break;
					}					
				default:
					break;
				}
			}
		}		
	}
	
	private class ListenerModifiedName implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			modifiedName();			
		}		
	}
	private class ListenerModifiedNotizen implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			modifiedNotizen();			
		}		
	}	

}
